package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.fragment.SmartAppliancesFragment;
import com.qiantu.phone.ui.fragment.SmartControlFragment;
import com.qiantu.phone.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * time   : 2021/12/22
 */
public final class SmartControlActivity extends AppActivity {

    private String[] tabs1;
    private String[] tabs2;
    private String[] currentTabs;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, SmartControlActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

//    private View titleView;
    private ViewPager viewPager;
    private ViewPagerIndicator tabBar;
    private List<Fragment> fragments;
    private FragmentManager mFragmentManager;
    private FragmentPagerAdapter mFragmentPagerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smart_control;
    }

    @Override
    protected void initView() {
        tabs1 = new String[]{getResources().getString(R.string.smart_appliances), getResources().getString(R.string.smart_control_system)};
        tabs2 = new String[]{getResources().getString(R.string.smart_appliances)};
        viewPager = findViewById(R.id.view_pager);
        tabBar = findViewById(R.id.tab_bar);
        ImmersionBar.setTitleBar(this, tabBar);
        currentTabs = tabs1;
        mFragmentManager = getSupportFragmentManager();
        initViewPager();
    }

    private void initViewPager() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    getTitleBar().getRightView().setVisibility(View.VISIBLE);
                }else{
                    getTitleBar().getRightView().setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabBar.setupViewpager(viewPager);
        tabBar.setTitles(currentTabs);
        fragments = new ArrayList<>();
        fragments.add(SmartAppliancesFragment.newInstance());
        fragments.add(SmartControlFragment.newInstance());
        mFragmentPagerAdapter = new FragmentPagerAdapter(mFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemPosition(@NonNull Object object) {
                return PagerAdapter.POSITION_NONE;
            }

            @Override
            public int getCount() {
                return currentTabs.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return null;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//                super.destroyItem(container, position, object);
            }
        };
        viewPager.setAdapter(mFragmentPagerAdapter);
        viewPager.setCurrentItem(0);
    }

    private void updateAdapter(){
        mFragmentPagerAdapter.notifyDataSetChanged();
        tabBar.setTitles(currentTabs);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onLeftClick(View view) {
        if(getTitleBar().getRightView().isSelected()){
            back();
            return;
        }
        finish();
    }

    @Override
    public void onRightClick(View view) {
        TextView rightView = getTitleBar().getRightView();
        if(rightView.getCompoundDrawables() != null && rightView.getCompoundDrawables()[2] != null){
            goEdit();
        }else if(!TextUtils.isEmpty(rightView.getText())){
            cancel();
        }
    }

    public void back(){
        getTitleBar().getRightView().setSelected(!getTitleBar().getRightView().isSelected());
        getTitleBar().setRightIcon(AppApplication.getInstance().getThemeDrawable(R.attr.icon_navigationbar_smartedit));
        getTitleBar().setRightTitle(null);
        ((SmartAppliancesFragment)fragments.get(0)).setSelectable(false);
        currentTabs = tabs1;
        updateAdapter();
    }

    public void goEdit(){
        getTitleBar().getRightView().setSelected(!getTitleBar().getRightView().isSelected());
        getTitleBar().setRightIcon(null);
        getTitleBar().setRightTitle(null);
        ((SmartAppliancesFragment)fragments.get(0)).setSelectable(true);
        currentTabs = tabs2;
        updateAdapter();
    }

    public void cancel(){
        getTitleBar().setRightIcon(null);
        getTitleBar().setRightTitle(null);
        ((SmartAppliancesFragment)fragments.get(0)).clearSelected();
    }

    public void showCancel(){
        getTitleBar().setRightIcon(null);
        getTitleBar().setRightTitle(R.string.cancel);
    }

    @Override
    public void onBackPressed() {
        if(getTitleBar().getRightView().isSelected()){
            back();
            return;
        }
        super.onBackPressed();
    }

}